﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;

namespace GovWaterApi
{
    public class GovTokenHelper
    { 

        private static AccessToken GetAccessToken() {
            var clientId = System.Configuration.ConfigurationManager.AppSettings["clientId"]??"shsw";
            var clientSecret = System.Configuration.ConfigurationManager.AppSettings["clientSecret"] ?? "shsw";
            var url = $"oauth2/getToken?clientId={clientId}&clientSecret={clientSecret}";
            return GetResult2<AccessToken>(url); 
        }

        public static string GetApplyNo(string itemCode)
        {
            var ac = GetAccessToken();
            var result=  GetResult<BaseResult<ApplyNo>>("portal/generateApplyNo", new { itemCode, accessToken = ac.access_token});
            if (result.isSuccess)
            {
                return result.data.applyNo;
            }
            return null;
        } 

     
        private static T GetResult<T>(string url,object data)
        {
            var baseUrl = System.Configuration.ConfigurationManager.AppSettings["tokenPath"];
            string strURL = baseUrl+url;
            System.Net.HttpWebRequest request;
            request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
            request.Method = "POST";
            request.ContentType = "application/json;charset=UTF-8";
            
            string paraUrlCoded = Newtonsoft.Json.JsonConvert.SerializeObject(data);
            byte[] payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
            request.ContentLength = payload.Length;
            using (Stream writer = request.GetRequestStream())
            {
                writer.Write(payload, 0, payload.Length);
                writer.Close();
            } 
            System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
            System.IO.Stream s= response.GetResponseStream();

            using (StreamReader Reader = new StreamReader(s, Encoding.UTF8))
            {
                string result = Reader.ReadToEnd();
                Reader.Close();
                return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(result);
            } 

        }

        private static string getTestPotalToken()
        {
            string strURL = "http://zwdtuser.sh.gov.cn/uc/oauth2.0/authorize.do?zwdtsw_user_id=be42771a-c033-4275-ad6c-7867eb908709";
            System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
            request.Method = "GET"; 
            System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
            System.IO.Stream s = response.GetResponseStream(); 
            StreamReader Reader = new StreamReader(s, Encoding.UTF8);

            string result = Reader.ReadToEnd();
            return result;
        }

        private static T GetResult2<T>(string url)
        {
            var baseUrl = System.Configuration.ConfigurationManager.AppSettings["tokenPath"];
            string strURL = baseUrl + url;
            System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
            System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
            System.IO.Stream s = response.GetResponseStream();

            StreamReader Reader = new StreamReader(s, Encoding.UTF8);

            string result = Reader.ReadToEnd();
            return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(result);
        }
    }

    public class BaseResult<T>  where T:class
    {
        public bool isSuccess { get; set; }

        public string code { get; set; }

        public string msg { get; set; }

        public string version { get; set; }
  
        public T data { get; set; }

    }

    public class AccessToken
    {
        public string access_token { get; set; }

        public int expires_in { get; set; }
         
    }

    public class ApplyNo
    {
        public string applyNo { get; set; }
    } 
}